package com.onenine.app.activity;

import android.graphics.Color;
import android.view.View;

import com.amap.api.location.AMapLocation;
import com.onenine.app.R;
import com.onenine.app.adapter.ExploreAdapter;
import com.onenine.app.event.NearbyConditionEvent;
import com.onenine.app.model.NearbyCondition;
import com.onenine.app.model.NearbyItem;
import com.onenine.app.request.NearbyRequest;
import com.onenine.app.response.NearbyResponse;
import com.onenine.app.utils.CDataProcessor;
import com.onenine.app.utils.GDLocation;
import com.onenine.app.utils.UrlConfig;
import com.onenine.app.view.cardstack.CardStack;
import com.onenine.base.base.BaseEventActivity;
import com.onenine.base.okhttp.OkCallback;
import com.onenine.base.okhttp.OkRequest;
import com.onenine.base.utils.StatusBarUtils;
import com.onenine.base.utils.Toasty;
import com.onenine.chat.rongcloud.RcUser;
import com.onenine.common.util.ActivityUtils;
import com.onenine.common.util.StringUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * 探索发现
 */
public class DiscoveryExploreActivity extends BaseEventActivity implements GDLocation.LocationCallback, CardStack.CardEventListener {

    private static final int MAX_CARD_COUNT = 20;
    private NearbyCondition mCondition;
    private AMapLocation mMapLocation;
    private CardStack mSwipeDeck;
    private ExploreAdapter mAdapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_discovery_explore;
    }

    @Override
    protected void initViews() {
        super.initViews();
        StatusBarUtils.setTransparent(this);
        mToolbar.setCenterTitle(R.string.explore);
        mToolbar.setRightTitle(R.string.filter);
        mToolbar.setBottomDividerVisible(false);
        mToolbar.setLeftDrawable(R.drawable.ic_back_white);
        mToolbar.setCenterTitleColor(Color.WHITE);
        mToolbar.setRightTitleColor(Color.WHITE);
        mToolbar.setLayoutBackgroundColor(Color.TRANSPARENT);
        mSwipeDeck = findViewById(R.id.cs_card);
        mSwipeDeck.setContentResource(R.layout.layout_explore_card_item);
        mAdapter = new ExploreAdapter();
        mSwipeDeck.setAdapter(mAdapter);
    }

    @Override
    protected void setStatusBarStyle() {
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(NearbyConditionEvent event) {
        //条件筛选查询
        mCondition = event.condition;
        loadData();
    }

    @Override
    protected void initData() {
        super.initData();
        showLoading();
        CDataProcessor.getLatestLocation(this);
    }

    @Override
    protected void initListeners() {
        super.initListeners();
        mSwipeDeck.setListener(this);
        mToolbar.setOnRightClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.startActivity(DiscoveryExploreActivity.this, NearbyConditionActivity.class);
            }
        });
    }

    @Override
    public void onResult(AMapLocation aMapLocation) {
        mMapLocation = aMapLocation;
        showLoading();
        loadData();
    }

    private void loadData() {
        if (mMapLocation == null) {
            return;
        }
        NearbyRequest request = new NearbyRequest();
        request.setUserId(RcUser.getInstance().getUserId());
        request.setLatitude(mMapLocation.getLatitude());
        request.setLongitude(mMapLocation.getLongitude());
        request.setPageNum(1);
        request.setPageSize(MAX_CARD_COUNT);
        if (mCondition != null) {
            request.setAgeb(mCondition.getAgeb());
            request.setAgee(mCondition.getAgee());
            request.setDistance(mCondition.getDistance());
            request.setSex(mCondition.getSex());
            request.setLabels(StringUtils.convertList2CommaStr(mCondition.getLabels()));
        }
        OkRequest.getInstance().withUrl(UrlConfig.EXPLORE_NEARBY)
                .withRequest(request).doRequest(DiscoveryExploreActivity.class.getSimpleName(), new OkCallback<NearbyResponse>() {
            @Override
            public void onResponse(NearbyResponse response, String srcData) {
                if (response.isSuccess()) {
                    if (response.getNearbyItemList().isEmpty()) {
                        Toasty.showNormal("未找到符合条件的内容");
                        return;
                    }
                    mAdapter.clear();
                    for (NearbyItem item : response.getNearbyItemList()) {
                        mAdapter.add(item);
                    }
                    mAdapter.notifyDataSetChanged();
                } else {
                    Toasty.showNormal(response.getMessage());
                }
                dismissLoading();
            }
        });
    }


    @Override
    public boolean swipeEnd(int section, float distance) {
        return true;
    }

    @Override
    public boolean swipeStart(int section, float distance) {
        return true;
    }

    @Override
    public boolean swipeContinue(int section, float distanceX, float distanceY) {
        return true;
    }

    @Override
    public void discarded(int mIndex, int direction) {
    }

    @Override
    public void topCardTapped() {
        NearbyItem item = (NearbyItem) mSwipeDeck.getTopView().getTag();
        if (item != null) {
            UserDetailActivity.launchActivity(this, item.getUserId());
        }
    }

}
